package com.tank.effect;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.Effect;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.texture.Texture;
import javafx.util.Duration;

public class ShipEffect extends Effect {

    private final Texture shipTexture;

    private boolean translateSet;

    public ShipEffect() {
        super(Duration.INDEFINITE);
        shipTexture = FXGL.texture("item/armed_ship.png");
        translateSet = false;
    }

    @Override
    public void onStart(Entity entity) {
        if (!translateSet) {
            shipTexture.setTranslateX(entity.getWidth() / 2.0 - shipTexture.getWidth() / 2.0);
            shipTexture.setTranslateY(entity.getHeight() / 2.0 - shipTexture.getHeight() / 2.0);
            translateSet = true;
        }
        entity.getViewComponent().addChild(shipTexture);
    }

    @Override
    public void onEnd(Entity entity) {
        entity.getViewComponent().removeChild(shipTexture);
    }
}
